import json
import threading
import time

import requests
# from memory_profiler import profile

from logger import log


class Settings:
    def __init__(self):
        self.streams = None
        self.platformId = None
        self.platformName = None
        self.name = None
        self.code = None
        self.id = None
        self.host = None

    def getConfig(self):
        while True:
            response = requests.get(url="https://" + self.host + "/api/weigh/internal/vsEdge/getByCode/" + self.code)
            ctx = response.text
            loads = json.loads(ctx)
            self.id = loads['data']['id']
            self.name = loads['data']['name']
            self.platformId = loads['data']['platformId']
            self.platformName = loads['data']['platformName']
            self.streams = loads['data']['ids']
            # log.info(f"配置加载完成:{loads}")
            time.sleep(60)

    # @profile
    def init(self, code, host):
        self.host = host
        self.code = code
        # 先执行一次再进入循环；
        response = requests.get(url="https://" + self.host + "/api/weigh/internal/vsEdge/getByCode/" + self.code)
        ctx = response.text
        loads = json.loads(ctx)
        self.id = loads['data']['id']
        self.name = loads['data']['name']
        self.platformId = loads['data']['platformId']
        self.platformName = loads['data']['platformName']
        self.streams = loads['data']['ids']

        thread = threading.Thread(target=self.getConfig)
        thread.start()


settings = Settings()
